//
//  SliderPageControlView.m
//  GridViewTest1
//
//  Created by Eidan Lin on 12-12-21.
//  Copyright (c) 2012年 taomm. All rights reserved.
//

#import "SliderControlView.h"
#import "UIView+EidanSize.h"


#define offSetX     -3
#define offSetY     -67

@implementation SliderControlView
@synthesize myControlSlider;
@synthesize popLabel;
@synthesize popImageView;
@synthesize sliderLabel;
@synthesize maxValue;
@synthesize delegate;
@synthesize thumbView;
@synthesize popBgView;
@synthesize myControlSliderWidth;


- (id)initWithFrame:(CGRect)frame andMinValue:(float )minValue andLeftBackgroundImageName:(NSString *)leftBackground andRightBg:(NSString *)rightBg andSliderBtnImageName:(NSString *)sliderBtn andLeftCapWidth:(float )leftCapWidth andTopCapHight:(float )topCapHeight andPopImageName:(NSString *)pop andPopViewMove:(int )popViewMoveFlag andSliderLabelDisplay:(int )sliderFlag{
    self = [super initWithFrame:frame];
    if (self) {
        self.minValue=minValue;
        self.leftCapWidth=leftCapWidth;
        self.topCapHeight=topCapHeight;
        self.isWidthChange=NO;
        
        if (pop == nil || [pop isEqualToString:@""]) {   //气泡是否显示
            self.isPopViewDisplay=0;
        }else{
            self.isPopViewDisplay=1;
        }
        
        if (self.isPopViewDisplay && popViewMoveFlag == 1) {
            self.isPopMove=1;
        }else{
            self.isPopMove=0;
        }
        
        if (self.isPopMove ==1 && sliderFlag) {
            self.isSliderLabelDisplay=1;
        }
        
        [self setUpWithLeftBackgroundImageName:leftBackground andRightBg:rightBg andSliderBtnImageName:sliderBtn andPopImageName:pop];
    }
    return self;
}


-(void)setUpWithLeftBackgroundImageName:(NSString *)leftBackground andRightBg:(NSString *)rightBg andSliderBtnImageName:(NSString *)sliderBtn andPopImageName:(NSString *)pop {
    
    if (self.subviews.count !=0) {  //clear
        [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    }
    
    self.myControlSlider=[[UISlider alloc] init];
    
    self.myControlSliderWidth=self.width;
    self.myControlSlider.frame=CGRectMake((self.width-self.myControlSliderWidth)/2, 0, self.myControlSliderWidth, self.height);
//    self.myControlSlider.backgroundColor = [UIColor whiteColor];
    
    self.myControlSlider.minimumValue=self.minValue;
    [self.myControlSlider setThumbImage:[UIImage imageNamed:sliderBtn] forState:UIControlStateNormal];
    [self.myControlSlider setThumbImage:[UIImage imageNamed:sliderBtn] forState:UIControlStateHighlighted];
    
    //只是为了获取滑块的宽度
    UIImageView *test=[[UIImageView alloc] initWithImage:[UIImage imageNamed:sliderBtn]];
    self.sliderBtnWidth=test.width;
    test=nil;
    
    //newpad，图要双倍，这边5,和3可能也要双倍。待测。,背景图一定只要能包括圆角就行，不能过长。
    [self.myControlSlider setMinimumTrackImage:[[UIImage imageNamed:leftBackground] stretchableImageWithLeftCapWidth:self.leftCapWidth topCapHeight:self.topCapHeight] forState:UIControlStateNormal];
    
    [self.myControlSlider setMaximumTrackImage:[[UIImage imageNamed:rightBg] stretchableImageWithLeftCapWidth:self.leftCapWidth topCapHeight:self.topCapHeight] forState:UIControlStateNormal];
    
    
    [self.myControlSlider addTarget:self action:@selector(touchUpToChangeValue:) forControlEvents:UIControlEventTouchUpInside];
    [self.myControlSlider addTarget:self action:@selector(touchUpToChangeValue:) forControlEvents:UIControlEventTouchUpOutside];
    [self.myControlSlider addTarget:self action:@selector(valueChageToDisplayPopView:) forControlEvents:UIControlEventValueChanged];
    self.myControlSlider.continuous=YES; //只要值改变连续触发事件
    
    
    [self addSubview:self.myControlSlider];
    
    self.popBgView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 62)];
    [self addSubview:self.popBgView];
    self.popBgView.top= offSetY;
    
    
    if (self.isPopViewDisplay) {
        self.popImageView=[[UIImageView alloc] initWithImage:[UIImage imageNamed:pop]];
        self.popImageView.left=(self.popBgView.width - self.popImageView.width)/2;
        self.popImageView.top=4;
        
        self.popLabel=[[UILabel alloc] initWithFrame:CGRectMake(0,11, self.popImageView.width, 20)];
        self.popLabel.text=@"1";
        self.popLabel.textAlignment=NSTextAlignmentCenter;
        self.popLabel.textColor=[UIColor whiteColor];
        self.popLabel.backgroundColor=[UIColor clearColor];
        [self.popLabel setShadowColor:[UIColor blackColor]];
        self.popLabel.font=[UIFont fontWithName:@"HelveticaNeue-Bold" size:14];
        [self.popLabel setShadowOffset:CGSizeMake(0, 1)];
        [self.popImageView addSubview:self.popLabel];
        [self.popBgView addSubview:self.popImageView];
        self.popImageView.hidden=YES;
    }
    
    if (self.isSliderLabelDisplay) {
        self.sliderLabel=[[UILabel alloc] initWithFrame:CGRectMake((self.popBgView.width-self.sliderBtnWidth)/2, -offSetY+1, self.sliderBtnWidth, 20)];
        self.sliderLabel.text=@"1";
        self.sliderLabel.textAlignment=NSTextAlignmentCenter;
        self.sliderLabel.textColor=[UIColor whiteColor];
        self.sliderLabel.font=[UIFont fontWithName:@"HelveticaNeue-Bold" size:14];
        [self.sliderLabel setShadowColor:[UIColor blackColor]];
        [self.sliderLabel setShadowOffset:CGSizeMake(0, 1)];
        self.sliderLabel.backgroundColor=[UIColor clearColor];
        [self.popBgView addSubview:self.sliderLabel];

    }
}
-(void)SliderControlSetUpMaxValue:(float )totalValue{
    self.myControlSlider.maximumValue=totalValue;
    self.maxValue=totalValue;
    
    if (self.isWidthChange) {  //宽度是否改变。。
        if (self.maxValue >25) {
            self.myControlSliderWidth=self.width;
        }else {
            self.myControlSliderWidth=self.maxValue/25.0*self.width;
        }
    }

    self.myControlSlider.frame=CGRectMake((self.width-self.myControlSliderWidth)/2, 0, self.myControlSliderWidth, self.height);
}

-(void)sliderControlResetWidth:(float)width{
    self.width = width;
    self.myControlSliderWidth = width;
    self.myControlSlider.frame=CGRectMake((self.width-self.myControlSliderWidth)/2, 0, self.myControlSliderWidth, self.height);
}

-(void)touchUpToChangeValue:(id)sender{
    if (self.isPopViewDisplay) {
        self.popImageView.hidden=YES;
    }
    
    [self performSelector:@selector(touchUpToChangeValueDelay) withObject:nil afterDelay:0.01];  //延迟一下，这样才能起到效果
}
-(void)touchUpToChangeValueDelay{
    float toValue=self.myControlSlider.value;
    if (self.isSliderLabelDisplay) {
        self.sliderLabel.hidden=NO;
        self.sliderLabel.text=[[NSString alloc] initWithFormat:@"%d",(int)toValue];
    }
    
    if ([self.delegate respondsToSelector:@selector(SliderControlHasToValue:)]) {
        [self.delegate SliderControlHasToValue:toValue];
    }
}
-(void)valueChageToDisplayPopView:(id)sender{
    float toValue=self.myControlSlider.value;
    if (self.isPopViewDisplay) {
        self.popImageView.hidden=NO;
        self.popLabel.text=[[NSString alloc] initWithFormat:@"%d页/%d页",(int)toValue,(int)self.maxValue];
    }
    if (self.isSliderLabelDisplay) {
        self.sliderLabel.hidden=YES;
    }
   
    if (self.isPopMove) {
        self.thumbView=[[self.myControlSlider subviews] objectAtIndex:2]; //获得滑块
        self.popBgView.left=self.thumbView.left+(self.width-self.myControlSliderWidth)/2-(self.popBgView.width-self.sliderBtnWidth)/2;
    }else{
        self.popBgView.left=(self.width-self.popBgView.width)/2;
    }
    
    if ([self.delegate respondsToSelector:@selector(SliderControlContinueToValue:)]) {
        [self.delegate SliderControlContinueToValue:toValue];
    }
    
}
//滑块跟着动
-(void) SliderControlSetUpToValue:(float) toValue{
    [self.myControlSlider setValue:toValue];
    if (self.myControlSlider.subviews.count >= 3) {
        self.thumbView=[[self.myControlSlider subviews] objectAtIndex:2]; //获得滑块
        if (self.isPopMove) {
            self.popBgView.left=self.thumbView.left+(self.width-self.myControlSliderWidth)/2-(self.popBgView.width-self.sliderBtnWidth)/2;
        }else{
            self.popBgView.left=(self.width-self.popBgView.width)/2;
        }
        if (self.isSliderLabelDisplay) {
            self.sliderLabel.text=[[NSString alloc] initWithFormat:@"%d",(int)toValue];
        }
    }
    
}

@end
